------------------
--代码参考:绯世⾏--
------------------
require("tuning")

-- 求上值
-- local function FindUpvalue(fn, upvalueName)
--     local i = 1
--     while true do
--         local name, value = debug.getupvalue(fn, i)
--         if not name then break end -- 没有更多的upvalue了
--         if name == upvalueName then
--             return value, i
--         end
--         i = i + 1
--     end
-- end

local spicedfoods = {}
-- local spicedfoods = require("spicedfoods")
-- local SPICES = FindUpvalue(GenerateSpicedFoods, "SPICES")
-- if not SPICES then return end -- 要是有人覆盖拿不到就不要调味了

local function oneaten_ssu_egg(inst, eater)
    eater:AddDebuff("tf_buff_ssu_egg", "tf_buff_ssu_egg")
end
local function oneaten_popping_candy(inst, eater)
    eater:AddDebuff("tf_buff_popping_candy", "tf_buff_popping_candy")
end
local function oneaten_takoyaki(inst, eater)
    eater:AddDebuff("tf_buff_takoyaki", "tf_buff_takoyaki")
end
local function oneaten_durian_candy(inst, eater)
    eater:AddDebuff("tf_buff_durian_candy", "tf_buff_durian_candy")
end
local function oneaten_dressing(inst, eater)
    eater:AddDebuff("tf_buff_dressing", "tf_buff_dressing")
end
local function oneaten_sweet_chili_sauce(inst, eater)
    eater:AddDebuff("buff_workeffectiveness", "buff_workeffectiveness")
    eater:AddDebuff("tf_buff_sweet_chili_sauce", "tf_buff_sweet_chili_sauce")
end
local function oneaten_coffee_powder(inst, eater)
    eater:AddDebuff("tf_buff_coffee_powder", "tf_buff_coffee_powder")
end
local function oneaten_bacon_bits(inst, eater)
    eater:AddDebuff("tf_buff_bacon_bits", "tf_buff_bacon_bits")
end
local function oneaten_strawberry_jam(inst, eater)
    eater:AddDebuff("tf_buff_strawberry_jam", "tf_buff_strawberry_jam")
end
local function oneaten_orange_jam(inst, eater)
    eater:AddDebuff("tf_buff_orange_jam", "tf_buff_orange_jam")
end
local function oneaten_protein_powder(inst, eater)
    eater:AddDebuff("tf_buff_protein_powder", "tf_buff_protein_powder")
end
local function oneaten_kimchi_powder(inst, eater)
    eater:AddDebuff("tf_buff_kimchi_powder", "tf_buff_kimchi_powder")
end

local TF_SPICES =
{
    SPICE_TF_KONPEITO = {},
    SPICE_TF_SUNNY_SIDE_UP_EGG = { oneatenfn = oneaten_ssu_egg, prefabs = { "tf_buff_ssu_egg" } },
    SPICE_TF_POPPING_CANDY = { oneatenfn = oneaten_popping_candy, prefabs = { "tf_buff_popping_candy" } },
    SPICE_TF_TAKOYAKI = { oneatenfn = oneaten_takoyaki, prefabs = { "tf_buff_takoyaki" } },
    SPICE_TF_DURIAN_CANDY = { oneatenfn = oneaten_durian_candy, prefabs = { "tf_buff_durian_candy" } },
    SPICE_TF_DRESSING = { oneatenfn = oneaten_dressing, prefabs = { "tf_buff_dressing" } },
    SPICE_TF_MAYONNAISE = {},
    SPICE_TF_MUSTARD = {},
    SPICE_TF_SWEET_CHILI_SAUCE = { oneatenfn = oneaten_sweet_chili_sauce, prefabs = { "buff_workeffectiveness", "tf_buff_sweet_chili_sauce" } },
    SPICE_TF_TOMATO_SAUCE = {},
    SPICE_TF_COFFEE_POWDER = { oneatenfn = oneaten_coffee_powder, prefabs = { "tf_buff_coffee_powder" } },
    SPICE_TF_BACON_BITS = { oneatenfn = oneaten_bacon_bits, prefabs = { "tf_buff_bacon_bits" } },
    SPICE_TF_ORANGE_JAM = { oneatenfn = oneaten_orange_jam, prefabs = { "tf_buff_orange_jam" } },
    SPICE_TF_STRAWBERRY_JAM = { oneatenfn = oneaten_strawberry_jam, prefabs = { "tf_buff_strawberry_jam" } },
    SPICE_TF_PROTEIN_POWDER = { oneatenfn = oneaten_protein_powder, prefabs = { "tf_buff_protein_powder" } },
    SPICE_TF_KIMCHI_POWDER = { oneatenfn = oneaten_kimchi_powder, prefabs = { "tf_buff_kimchi_powder" } },
}

-- for k, _ in pairs(TF_SPICES) do
--     local spicename = string.lower(k)
-- end

-- 把我们的调味料追加到科雷这个表里，这样其他mod的料理也能调味
-- for k, v in pairs(TF_SPICES) do
--     SPICES[k] = v
-- end

--获取新的调味料理配方
local function TfGetNewRecipes()
    return spicedfoods
end

-- 调味生成函数
local function Tf_GenerateSpicedFoods(foods)
    for foodname, fooddata in pairs(foods) do
        if not (fooddata.spice or fooddata.notinitprefab) then
            for spicenameupper, spicedata in pairs(TF_SPICES) do
                local newdata = shallowcopy(fooddata)
                if newdata == nil then
                    return
                end
                local spicename = string.lower(spicenameupper)
                if foodname == "wetgoop" then
                    newdata.test = function(cooker, names, tags) return names[spicename] end
                    newdata.priority = -10
                else
                    newdata.test = function(cooker, names, tags) return names[foodname] and names[spicename] end
                    newdata.priority = 100
                end
                newdata.cooktime = .12
                newdata.stacksize = nil
                newdata.spice = spicenameupper
                newdata.basename = foodname
                newdata.name = foodname .. "_" .. spicename
                newdata.floater = { "med", nil, { 0.85, 0.7, 0.85 } }
                newdata.official = false
                newdata.cookbook_category = fooddata.cookbook_category ~= nil and ("spiced_" .. fooddata.cookbook_category) or nil
                if IsLgOrMythFood(newdata.basename) then
                    newdata.overridebuild = newdata.basename
                end
                spicedfoods[newdata.name] = newdata

                if spicedata.prefabs ~= nil then
                    newdata.prefabs = newdata.prefabs ~= nil and ArrayUnion(newdata.prefabs, spicedata.prefabs) or spicedata.prefabs
                end

                if spicedata.oneatenfn ~= nil then
                    if newdata.oneatenfn ~= nil then
                        local oneatenfn_old = newdata.oneatenfn
                        newdata.oneatenfn = function(inst, eater)
                            spicedata.oneatenfn(inst, eater)
                            oneatenfn_old(inst, eater)
                        end
                    else
                        newdata.oneatenfn = spicedata.oneatenfn
                    end
                end
            end
        end
    end
end

-- 因为需要生成我们的调味料调味后料理，所有料理执行 Tf_GenerateSpicedFoods 函数
-- for k, v in pairs(cooking.recipes) do
--     if k and v and k ~= "portablespicer" then
--         Tf_GenerateSpicedFoods(v)
--     end
-- end

-- 调味后料理加到便携香料站里
-- for k, recipe in pairs(spicedfoods) do
--     if TF_SPICES[recipe.spice] then
--         AddCookerRecipe("portablespicer", recipe)
--     end
-- end

return {
    spicedfoods = spicedfoods,
    Tf_GenerateSpicedFoods = Tf_GenerateSpicedFoods,
    TfGetNewRecipes = TfGetNewRecipes,
    TF_SPICES = TF_SPICES,
}
